package com.jstyle.beatecg.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.util.ResolveData;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/4.
 */

public class MyPointView extends RelativeLayout {
    Paint paint;
    Paint linePaint;
    public MyPointView(Context context) {
        super(context);
    }

    public MyPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        init();
    }

    private void init() {
        paint=new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);

        linePaint=new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setStrokeWidth(1);
        linePaint.setTextSize(50);
        linePaint.setColor(getContext().getResources().getColor(R.color.main_color));

    }
    int height=32;
    public  void updateData(List<Integer>data,int height){
        this.list=data;
        this.height=height;
        postInvalidate();
    }


    public MyPointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        if(height==32){
//            drawPoints(canvas);
//        }else{
//            drawPointsVer(canvas);
//        }
        drawRect(canvas);
       drawText(canvas,"25mm/s 10mm/mv",getWidth()-20,getHeight()/2);
      //  drawPoints(canvas);
        // canvas.drawPoint();

    }
    void drawText(Canvas canvas,String text,float x,float y){
        Rect rect = new Rect();
        linePaint.getTextBounds(text,0,text.length(),rect);
        int width = rect.width();//文本宽度
        canvas.rotate(-90,x,y+width/2);
        canvas.drawText(text,x,y+width/2,linePaint);
        canvas.rotate(90,x,y+width/2);

    }
    float count=125;
    LinkedList<Float>listData=new LinkedList<>();
    private void drawRect(Canvas canvas){
        float width=getWidth()/count;
        for(int i=0;i<60;i++){
            float y=width*i;
            if(i%5==0){
                paint.setColor(Color.RED);
                canvas.drawLine(0,y,getWidth(),y,paint);
            }
            for(int j=0;j<count;j++){
                paint.setColor(Color.WHITE);
                float x=j*width;
                canvas.drawPoint(x,y,paint);
                if(j%5==0){
                    paint.setColor(Color.RED);
                    canvas.drawLine(x,y,x,y+width,paint);
                }
            }
        }
        paint.setColor(Color.RED);
        canvas.drawLine(0,width*60,getWidth(),width*60,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) (width/125f*60f);
        Log.i(TAG, "onMeasure: "+height);
        setMeasuredDimension(width,height);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged: "+h+" "+oldh);
    }
    //    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
//        Log.i(TAG, "onLayout: ");
//    }

    List<Integer> list = new ArrayList<Integer>();

    private void drawPoints(Canvas canvas) {
        Path path=new Path();
        float HEIGHT=getHeight();
        float widthX=getWidth()/count;
        for(int i=0;i<listData.size();i++){
            float value=listData.get(i);
            Log.i(TAG, "drawPoints: "+value);
            path.lineTo(i*widthX,HEIGHT-(value/6f)*HEIGHT);
        }
        canvas.drawPath(path,linePaint);

    }
    public  void addData(byte[]value){
        for(int i=1;i<9;i++){
            float data = ResolveData.getValue(value[i * 2 + 1], 1) + ResolveData.getValue(value[i * 2], 0);
            if(listData.size()>=1000)listData.poll();
            listData.add(data/400f);
        }
        postInvalidate();
    }

    private static final String TAG = "MyPointView";
    private void drawPointsVer(Canvas canvas) {
        if(list.size()<1024)return;
        float width=1;
        float height=1;
        for (int i = 0; i < this.height * 16; i++) {
            float x = i % 64;
            float y = i / 64 * 8;
            int data = list.get(i);
            for (int j = 0; j < 8; j++) {
                int temp = (byte) (data & 1);
                data = (byte) (data >> 1);
                int color;
                if (temp == 1) {
                    color = Color.BLACK;
                } else {
                    color = Color.WHITE;
                }
                paint.setColor(color);
                float cx=x*width;
                float cy=(y+j)*height;
                canvas.drawRect(cx,cy,cx+height,cy+height,paint);
                // canvas.drawCircle(cx+height/2f,cy+height/2f ,height/2f, paint);

            }
        }
        Log.i(TAG, "drawPoints: ");
    }
}
